package com.lineage.server.command.executor;

import com.lineage.server.datatables.NpcTable;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.Instance.L1PetInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Npc;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1Summon implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1Summon.class);
  
  public static L1Summon getInstance() {
    return new L1Summon();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      StringTokenizer tok = new StringTokenizer(arg);
      String nameid = tok.nextToken();
      int npcid = 0;
      try {
        npcid = Integer.parseInt(nameid);
      } catch (NumberFormatException e) {
        npcid = NpcTable.get().findNpcIdByNameWithoutSpace(nameid);
        if (npcid <= 0) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "错误的NPCID: " + npcid));
          return;
        } 
      } 
      int count = 1;
      if (tok.hasMoreTokens())
        count = Integer.parseInt(tok.nextToken()); 
      if (count > 5) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("宠物召唤数量一次禁止超过5只。"));
        return;
      } 
      L1Npc npc = NpcTable.get().getTemplate(npcid);
      int i = 0;
      while (i < count) {
        L1PetInstance pet = new L1PetInstance(npc, pc);
        pet.setPetcost(0);
        i++;
      } 
      nameid = NpcTable.get().getTemplate(npcid).get_name();
      pc.sendPackets(
          (ServerBasePacket)new S_ServerMessage(166, String.valueOf(String.valueOf(nameid)) + "(ID:" + npcid + ") 数量:" + count + " 完成召唤"));
    } catch (Exception e2) {
      _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
    } 
  }
}
